﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace FluentWebRequest
{
    internal class RequestItemName : IRequestItemName
    {
        private readonly IRequestDataBuilder _requestDataBuilder;
        private readonly IDictionary<string, string> _requestItems;
        private readonly string _url;
        private readonly string _name;

        public RequestItemName(IRequestDataBuilder requestDataBuilder, string url, IDictionary<string, string> requestItems, string name)
        {
            _requestDataBuilder = requestDataBuilder;
            _url = url;
            _requestItems = requestItems;
            _name = name;
        }

        public IAndConnection ValueIs(string value)
        {
            // find & remove first
            var key = _requestItems.Keys.FirstOrDefault(x => x.Equals(_name, StringComparison.CurrentCulture));
            _requestItems.Remove(key);
            //add new
            _requestItems.Add(_name, value);

            return new AndConnection(_requestDataBuilder, _url, _requestItems);
        }
    }
}